﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CaiyinSoft.NetGather.NetGatherUtility
{
    public class HyperLink
    {
        public string Title { get; set; }
        public string Href { get; set; }
        public string FullHref 
        {
            get
            {
                return HyperLinkUtility.FullUrl(RootUrl, Href);
            }
        }

        public Dictionary<string, string> Attrs { get; set; }

        public string RootUrl { get; set; }

        public HyperLink()
        {
            Title = "";
            Href = "";
            Attrs = new Dictionary<string, string>();
        }

        public HyperLink(string Title, string Href, string  RootUrl)
        {
            this.Title = Title;
            this.Href = Href;
            this.RootUrl = RootUrl;
            Attrs = new Dictionary<string, string>();
        }


    }


    public class HyperLinkUtility
    {
        public static string FullUrl(string OrgUrl, string PageUrl)
        {

            if (PageUrl.Trim().Length == 0) return "";
            if (PageUrl.Contains("http://")) return PageUrl;
            if (PageUrl.Contains("https://")) return PageUrl;
            if (OrgUrl==null || OrgUrl.Trim().Length == 0) return PageUrl;

            string reValue = "";

            Uri uri = new Uri(OrgUrl);
            string httproot = uri.Scheme + "://" + uri.Host;
            if (PageUrl.Substring(0, 1) == "/")
            {
                reValue = httproot + PageUrl;
            }
            else
            {
                int maxSegment = uri.Segments.Count() ;
                if (!uri.Segments[uri.Segments.Length - 1].EndsWith("/"))
                {
                    maxSegment--;
                }
                reValue = httproot;
                for (int i = 0; i < maxSegment; i++)
                {
                    reValue += uri.Segments[i];
                }

                reValue += PageUrl;
            }

            return reValue;
        }


        public static void RemovePicAndAFromHTML(HtmlAgilityPack.HtmlNode parentNode)
        {
            RemovePicAndAFromHTML(parentNode, false);
        }

        public static void RemovePicAndAFromHTML( HtmlAgilityPack.HtmlNode parentNode,bool removeScript)
        {

            foreach (HtmlAgilityPack.HtmlNode a in parentNode.Elements("a"))
            {
                if (a.Attributes["href"] != null)
                    a.Attributes["href"].Remove();
            }

            foreach (HtmlAgilityPack.HtmlNode img in parentNode.Elements("img"))
            {
                if (img.Attributes["src"] != null)
                {
                    if (img.Attributes["src"].Value.Contains("taobao") || img.Attributes["src"].Value.Contains("ali"))
                        img.Attributes["src"].Remove();
                }
            }

            if (removeScript)
            {
                System.Collections.Generic.IEnumerable<HtmlAgilityPack.HtmlNode> scripts = parentNode.Elements("script");
                foreach (HtmlAgilityPack.HtmlNode script in scripts)
                {
                    script.InnerHtml = "";
                }
            }


            foreach (HtmlAgilityPack.HtmlNode link in parentNode.ChildNodes)
            {
                RemovePicAndAFromHTML(link,removeScript);
            }

        }
    }
}
